/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Theme_Builder
{
    public partial class EditLineStyle : UserControl
    {
        public EditLineStyle()
        {
            InitializeComponent();

            this.panelLineColor.BackColor = Color.White;
            this.comboBoxLineColor.SelectedIndex = 0;
            this.numericUpDownWeight.Value = (decimal)1;
            this.comboBoxStyle.SelectedIndex = 0;
            this.comboBoxCompound.SelectedIndex = 0;
            this.comboBoxCap.SelectedIndex = 0;
            this.comboBoxAlignment.SelectedIndex = 0;
        }

        public OfficeOpenXMLInterop.CT_LineProperties LineStyle
        {
            get
            {
                OfficeOpenXMLInterop.CT_LineProperties lineStyle = new OfficeOpenXMLInterop.CT_LineProperties();
                if (comboBoxLineColor.Text == "Placeholder Color")
                {
                    // We're using the placeholder color
                    lineStyle.solidFill = new OfficeOpenXMLInterop.CT_SolidColorFillProperties();
                    lineStyle.solidFill.schemeClr = new OfficeOpenXMLInterop.CT_SchemeColor();
                    lineStyle.solidFill.schemeClr.val = OfficeOpenXMLInterop.ST_SchemeColorVal.phClr;
                    Converters.ParseModifications(ref lineStyle.solidFill.schemeClr, this.editColorMods.ColorModifications);
                }
                else
                {
                    // We're using a custom color
                    lineStyle.solidFill = new OfficeOpenXMLInterop.CT_SolidColorFillProperties();
                    lineStyle.solidFill.srgbClr = new OfficeOpenXMLInterop.CT_SRgbColor();
                    lineStyle.solidFill.srgbClr.val = Converters.ColorToByteRBG(this.panelLineColor.BackColor);
                }
                // TODO: it's possible to have other colors here, but we only really handle a phClr and custom color
                //  all over in the tool.  We should update this to work with all the color types within the defined schema

                lineStyle.w = (int)Math.Round(this.numericUpDownWeight.Value * 12700); ;
                lineStyle.wSpecified = true;
                if (this.comboBoxStyle.Text != "")
                {
                    lineStyle.prstDash = Converters.GetLineDashProps(this.comboBoxStyle.Text);
                    lineStyle.prstDash.valSpecified = true;
                }
                if (this.comboBoxCompound.Text != "")
                {
                    lineStyle.cmpd = Converters.GetCompoundLine(this.comboBoxCompound.Text);
                    lineStyle.cmpdSpecified = true;
                }
                if (this.comboBoxCap.Text != "")
                {
                    lineStyle.cap = Converters.GetLineCap(this.comboBoxCap.Text);
                    lineStyle.capSpecified = true;
                }
                if (this.comboBoxAlignment.Text != "")
                {
                    lineStyle.algn = Converters.GetAlignment(this.comboBoxAlignment.Text);
                    lineStyle.algnSpecified = true;
                }
                return lineStyle;
            }
            set
            {
                OfficeOpenXMLInterop.CT_LineProperties lineStyle = (OfficeOpenXMLInterop.CT_LineProperties)value;
                if (lineStyle == null)
                    return;

                // See if we are using phClr
                if (lineStyle.solidFill != null && lineStyle.solidFill.schemeClr != null)
                {
                    this.comboBoxLineColor.SelectedIndex = 0;
                    this.editColorMods.ColorModifications = Converters.GetColorModifications(lineStyle.solidFill.schemeClr);
                }
                else if (lineStyle.solidFill != null && lineStyle.solidFill.srgbClr != null)
                {
                    this.comboBoxLineColor.SelectedIndex = 1;
                    this.panelLineColor.BackColor = Converters.ByteRGBToColor(lineStyle.solidFill.srgbClr.val);
                }
                else
                    System.Diagnostics.Debug.Assert(false, "Unsupported color format");

                this.numericUpDownWeight.Value = (decimal)((float)lineStyle.w / 12700f);

                if (lineStyle.prstDash != null)
                    this.comboBoxStyle.Text = Converters.GetLineDashProps(lineStyle.prstDash);

                this.comboBoxCompound.Text = Converters.GetCompoundLine(lineStyle.cmpd);
                this.comboBoxCap.Text = Converters.GetLineCap(lineStyle.cap);
                this.comboBoxAlignment.Text = Converters.GetAlignment(lineStyle.algn);
            }
        }

        private void comboBoxLineColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Enable tint and shade if we use a placeholder color, and the color panel otherwise.
            bool fPhClr = this.comboBoxLineColor.SelectedIndex == 0;  // placeholder color
            this.panelLineColor.Visible = !fPhClr;
            this.editColorMods.Enabled = fPhClr;
        }

        private void panelLineColor_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();
            dlg.FullOpen = true;
            dlg.AnyColor = true;
            dlg.Color = this.panelLineColor.BackColor;
            if (dlg.ShowDialog(this) == DialogResult.OK)
                this.panelLineColor.BackColor = dlg.Color;
        }
    }
}
